package com.apes.framework.plugin.fsm;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Objects;

/**
 * 功能：状态事件
 *
 * @author xul
 * @create 2020-08-27 11:37
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FsmEvent.CONST_TABLE_NAME)
public class FsmEvent extends BaseModel {
    public static final String CONST_TABLE_NAME = "fsm_event";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "状态流：事件")
    @Comment("编码")
    private String id;

    @Column(name = "name", nullable = false, length = 60)
    @Comment("事件名称")
    private String name;

    @ManyToOne(optional = false)
    @JoinColumn(name = "fsm_flow_id", referencedColumnName = "id", nullable = false)
    @Comment("状态流")
    private FsmFlow fsmFlow;

    @Column(name = "when_mvel")
    @Comment("事件条件")
    private String whenMvel;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("是否有效")
    private boolean valid = true;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FsmEvent event = (FsmEvent) o;
        return Objects.equals(id, event.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return name;
    }
}
